package coml.jiyun.hp.day14_17and20.fragment;


import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import coml.jiyun.hp.day14_17and20.R;
import coml.jiyun.hp.day14_17and20.adapter.RlvHomeAdapter;
import coml.jiyun.hp.day14_17and20.bean.BannerBean;
import coml.jiyun.hp.day14_17and20.bean.WanBean;
import coml.jiyun.hp.day14_17and20.model.ImpBannerModel;
import coml.jiyun.hp.day14_17and20.model.ImpWanModel;
import coml.jiyun.hp.day14_17and20.presenter.ImpBannerPresenter;
import coml.jiyun.hp.day14_17and20.presenter.ImpWanPresenter;
import coml.jiyun.hp.day14_17and20.view.BannView;
import coml.jiyun.hp.day14_17and20.view.WanView;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment implements WanView, BannView {


    private RecyclerView mRlv;
    private ArrayList<WanBean.DataBean.DatasBean> datasBeans;
    private RlvHomeAdapter rlvHomeAdapter;

    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_home, container, false);
        initView(inflate);
        initData();
        return inflate;
    }

    private void initData() {
        ImpWanPresenter impWanPresenter = new ImpWanPresenter(new ImpWanModel(), this);
        impWanPresenter.getData();

        ImpBannerPresenter impBannerPresenter = new ImpBannerPresenter(new ImpBannerModel(), this);
        impBannerPresenter.getData();
    }

    private void initView(View inflate) {
        mRlv = inflate.findViewById(R.id.home_rlv);


        datasBeans = new ArrayList<>();
        rlvHomeAdapter = new RlvHomeAdapter(getActivity(), datasBeans);
        mRlv.setAdapter(rlvHomeAdapter);
        mRlv.setLayoutManager(new LinearLayoutManager(getActivity()));

    }

    @Override
    public void onSuccess(WanBean wanBean) {
        datasBeans.addAll(wanBean.getData().getDatas());
        Log.d("TAG", "onSuccess: "+datasBeans);
        rlvHomeAdapter.notifyDataSetChanged();
    }

    @Override
    public void onSuccess(BannerBean bannerBean) {
        List<BannerBean.DataBean> data = bannerBean.getData();
        rlvHomeAdapter.setBannerBean(data);

    }

    @Override
    public void onFail(String str) {

    }
}
